<?php

/**
 * 冲上云霄模块
 */
defined('IN_IA') or exit('Access Denied');

//include "model.php";

class Laoyou_csyxModule extends WeModule {

    public $tablename = 'laoyou_csyx_reply';

    public function fieldsFormDisplay($rid = 0) {
        global $_W;

        load()->func('tpl');

        if (!empty($rid)) {
            $reply = pdo_fetch("SELECT * FROM " . tablename($this->tablename) . " WHERE rid = :rid", array(':rid' => $rid));
        }
        if (!$reply) {
            $reply = array(
                'img_url' => './addons/laoyou_csyx/template/style/homeBg.jpg',
                's_img' => './addons/laoyou_csyx/template/style/homeBg.jpg',
                'rule' => '',
                'times' => 0,
            );
        }

        include $this->template('form');
    }

    public function fieldsFormValidate($rid = 0) {
        //规则编辑保存时，要进行的数据验证，返回空串表示验证无误，返回其他字符串将呈现为错误提示。这里 $rid 为对应的规则编号，新增时为 0
        return '';
    }

    public function fieldsFormSubmit($rid) {
        global $_GPC, $_W;
        $id = intval($_GPC['reply_id']);
        $insert = array(
            'rid' => $rid,
            'weid' => $_W['weid'],
            'title' => $_GPC['title'],
            'score' => $_GPC['score'],
            'reply' => $_GPC['success_stitle'],
            'r_title' => $_GPC['r_title'],
            'img_url' => $_GPC['img_url'],
            'people_num' => $_GPC['people_num'],
            'wnum' => $_GPC['wnum'],
            's_title' => $_GPC['s_title'],
            's_img' => $_GPC['s_img'],
            's_content' => $_GPC['s_content'],
            'content' => $_GPC['content'],
            'zcontent' => $_GPC['zcontent'],
            'addnum' => $_GPC['addnum'],
            'pirzenum' => $_GPC['pirzenum'],
            'repeat' => $_GPC['repeat'],
            'starttime' => strtotime($_GPC['datelimit']['start']),
            'endtime' => strtotime($_GPC['datelimit']['end']),
            'subscribeurl' => $_GPC['subscribeurl'],
            'c_type_one' => $_GPC['c_type_one'],
            'c_name_one' => $_GPC['c_name_one'],
            'c_num_one' => $_GPC['c_num_one'],
            'c_type_two' => $_GPC['c_type_two'],
            'c_name_two' => $_GPC['c_name_two'],
            'c_num_two' => $_GPC['c_num_two'],
            'c_type_three' => $_GPC['c_type_three'],
            'c_name_three' => $_GPC['c_name_three'],
            'c_num_three' => $_GPC['c_num_three'],
            'c_type_four' => $_GPC['c_type_four'],
            'c_name_four' => $_GPC['c_name_four'],
            'c_num_four' => $_GPC['c_num_four'],
            'c_type_five' => $_GPC['c_type_five'],
            'c_name_five' => $_GPC['c_name_five'],
            'c_num_five' => $_GPC['c_num_five'],
            'c_type_six' => $_GPC['c_type_six'],
            'c_name_six' => $_GPC['c_name_six'],
            'c_num_six' => $_GPC['c_num_six'],
            'c_rate_one' => $_GPC['c_rate_one'],
            'c_rate_two' => $_GPC['c_rate_two'],
            'c_rate_three' => $_GPC['c_rate_three'],
            'c_rate_four' => $_GPC['c_rate_four'],
            'c_rate_five' => $_GPC['c_rate_five'],
            'c_rate_six' => $_GPC['c_rate_six'],
            'cover_img' => $_GPC['cover_img'],
            'hero_img_up' => $_GPC['hero_img_up'],
            'hero_img_down' => $_GPC['hero_img_down'],
            'abnormal' => $_GPC['abnormal'],
            'abnormal_manage' => $_GPC['abnormalManage']
        );

        if (!empty($_GPC['img_url'])) {
            $insert['img_url'] = $_GPC['img_url'];
            //file_delete($_GPC['img_url-old']);
        }

        if (empty($id)) {
            $id = pdo_insert($this->tablename, $insert);
        } else {
            pdo_update($this->tablename, $insert, array('id' => $id));
        }

        return true;
    }

    public function ruleDeleted($rid) {
        pdo_delete('laoyou_csyx_reply', array('rid' => $rid));
        pdo_delete('laoyou_csyx_record', array('rid' => $rid));
        pdo_delete('laoyou_csyx_winning', array('rid' => $rid));
        pdo_delete('laoyou_csyx_share', array('rid' => $rid));
        pdo_delete('laoyou_csyx_fans', array('rid' => $rid));
        pdo_delete('laoyou_csyx_tx', array('rid' => $rid));
    }

    public function dodelete() {
        global $_GPC, $_W;
        $rid = intval($_GPC['rid']);
        $rule = pdo_fetch("SELECT id, module FROM " . tablename('rule') . " WHERE id = :id and weid=:weid", array(':id' => $rid, ':weid' => $_W['weid']));
        if (empty($rule)) {
            message('抱歉，要修改的规则不存在或是已经被删除！');
        }
        if (pdo_delete('rule', array('id' => $rid))) {
            pdo_delete('rule_keyword', array('rid' => $rid));
            //删除统计相关数据
            pdo_delete('stat_rule', array('rid' => $rid));
            pdo_delete('stat_keyword', array('rid' => $rid));
            //调用模块中的删除
            $module = WeUtility::createModule($rule['module']);
            if (method_exists($module, 'ruleDeleted')) {
                $module->ruleDeleted($rid);
            }
        }


        message('规则操作成功！', create_url('site/module/manage', array('name' => 'laoyou_csyx')), 'success');
    }

    public function dodeleteAll() {
        global $_GPC, $_W;

        foreach ($_GPC['idArr'] as $k => $rid) {
            $rid = intval($rid);
            if ($rid == 0)
                continue;
            $rule = pdo_fetch("SELECT id, module FROM " . tablename('rule') . " WHERE id = :id and weid=:weid", array(':id' => $rid, ':weid' => $_W['weid']));
            if (empty($rule)) {
                $this->message('抱歉，要修改的规则不存在或是已经被删除！');
            }
            if (pdo_delete('rule', array('id' => $rid))) {
                pdo_delete('rule_keyword', array('rid' => $rid));
                //删除统计相关数据
                pdo_delete('stat_rule', array('rid' => $rid));
                pdo_delete('stat_keyword', array('rid' => $rid));
                //调用模块中的删除
                $module = WeUtility::createModule($rule['module']);
                if (method_exists($module, 'ruleDeleted')) {
                    $module->ruleDeleted($rid);
                }
            }
        }
        $this->message('规则操作成功！', '', 0);
    }

    // public function dodownload() {
    //     require_once 'download.php';
    // }
    // public function dosetshow() {
    //     global $_GPC, $_W;
    //     $rid = intval($_GPC['rid']);
    //     $isshow = intval($_GPC['isshow']);
    //     if (empty($rid)) {
    //         message('抱歉，传递的参数错误！', '', 'error');
    //     }
    //     $temp = pdo_update('bigwheel_reply', array('isshow' => $isshow), array('rid' => $rid));
    //     message('状态设置成功！', create_url('site/module/manage', array('name' => 'bigwheel')), 'success');
    // }
    // public function dosetstatus() {
    //     global $_GPC, $_W;
    //     $id = intval($_GPC['id']);
    //     $status = intval($_GPC['status']);
    //     if (empty($id)) {
    //         message('抱歉，传递的参数错误！', '', 'error');
    //     }
    //     $p = array('status' => $status);
    //     if ($status == 2) {
    //         $p['consumetime'] = TIMESTAMP;
    //     }
    //     $temp = pdo_update('bigwheel_award', $p, array('id' => $id, 'weid' => $_W['weid']));
    //     if ($temp == false) {
    //         message('抱歉，刚才操作数据失败！', '', 'error');
    //     } else {
    //         message('状态设置成功！', create_url('site/module/awardlist', array('name' => 'bigwheel', 'rid' => $_GPC['rid'])), 'success');
    //     }
    // }
    // public function dogetphone() {
    //     global $_GPC, $_W;
    //     $rid = intval($_GPC['rid']);
    //     $fans = $_GPC['fans'];
    //     $tel = pdo_fetchcolumn("SELECT tel FROM " . tablename('bigwheel_fans') . " WHERE rid = " . $rid . " and  from_user='" . $fans . "'");
    //     if ($tel == false) {
    //         echo '没有登记';
    //     } else {
    //         echo $tel;
    //     }
    // }

    public function message($error, $url = '', $errno = -1) {
        $data = array();
        $data['errno'] = $errno;
        if (!empty($url)) {
            $data['url'] = $url;
        }
        $data['error'] = $error;
        echo json_encode($data);
        exit;
    }

    /* 	public function doManage() {
      global $_GPC, $_W;
      include model('rule');
      $pindex = max(1, intval($_GPC['page']));
      $psize = 20;
      $sql = "weid = :weid AND `module` = :module";
      $params = array();
      $params[':weid'] = $_W['weid'];
      $params[':module'] = 'laoyou_csyx';

      if (isset($_GPC['keywords'])) {
      $sql .= ' AND `name` LIKE :keywords';
      $params[':keywords'] = "%{$_GPC['keywords']}%";
      }
      $list = rule_search($sql, $params, $pindex, $psize, $total);
      $pager = pagination($total, $pindex, $psize);

      if (!empty($list)) {
      foreach ($list as &$item) {
      $condition = "`rid`={$item['id']}";
      $item['keywords'] = rule_keywords_search($condition);
      $laoyou_csyx = pdo_fetch("SELECT * FROM " . tablename('laoyou_csyx_reply') . " WHERE rid = :rid ", array(':rid' => $item['id']));
      $fans = pdo_fetchall("SELECT * FROM " . tablename('laoyou_csyx_fans'). "WHERE rid = " .$item['id']);
      $item['fansnum'] = count($fans);
      $item['starttime'] = date('Y-m-d H:i', $laoyou_csyx['starttime']);
      $endtime = $laoyou_csyx['endtime'] + 86399;
      $item['endtime'] = date('Y-m-d H:i', $endtime);
      $nowtime = time();
      if ($laoyou_csyx['starttime'] > $nowtime) {
      $item['status'] = '<span class="label label-red">未开始</span>';
      $item['show'] = 1;
      } elseif ($endtime < $nowtime) {
      $item['status'] = '<span class="label label-blue">已结束</span>';
      $item['show'] = 0;
      }
      }
      }
      include $this->template('manage');
      }

      public function doawardlist() {
      global $_GPC, $_W;
      $rid = intval($_GPC['rid']);
      if (empty($rid)) {
      message('抱歉，传递的参数错误！', '', 'error');
      }
      $where = '';
      $params = array(':rid' => $rid, ':weid' => $_W['weid']);
      if (!empty($_GPC['status'])) {
      $where.=' and a.status=:status';
      $params[':status'] = $_GPC['status'];
      }
      if (!empty($_GPC['keywords'])) {
      $where.=' and (a.award_sn like :award_sn)';
      $params[':award_sn'] = "%{$_GPC['keywords']}%";
      }

      $total = pdo_fetchcolumn("SELECT count(a.id) FROM " . tablename('laoyou_csyx_winning') . " a left join " . tablename('laoyou_csyx_fans') . " f on a.from_user=f.openid WHERE a.rid = :rid and a.weid=:weid " . $where . "", $params);
      $pindex = max(1, intval($_GPC['page']));
      $psize = 12;
      $pager = pagination($total, $pindex, $psize);
      $start = ($pindex - 1) * $psize;
      $limit .= " LIMIT {$start},{$psize}";
      $list = pdo_fetchall("SELECT a.* FROM " . tablename('laoyou_csyx_winning') . " a left join " . tablename('laoyou_csyx_fans') . " f on a.from_user=f.openid WHERE a.rid = :rid and a.weid=:weid  " . $where . " ORDER BY a.id DESC " . $limit, $params);

      //一些参数的显示
      $num1 = pdo_fetchcolumn("SELECT sum(total) FROM " . tablename('laoyou_csyx_award') . " WHERE rid = :rid", array(':rid' => $rid));
      $num2 = pdo_fetchcolumn("SELECT count(id) FROM " . tablename('laoyou_csyx_winning') . " WHERE rid = :rid and status=1", array(':rid' => $rid));
      $num3 = pdo_fetchcolumn("SELECT count(id) FROM " . tablename('laoyou_csyx_winning') . " WHERE rid = :rid and status=2", array(':rid' => $rid));

      include $this->template('awardlist');
      }

      public function dodownload() {
      require_once 'download.php';
      }

      public function dosetstatus() {
      global $_GPC, $_W;
      $id = intval($_GPC['id']);
      $status = intval($_GPC['status']);
      if (empty($id)) {
      message('抱歉，传递的参数错误！', '', 'error');
      }
      $p = array('status' => $status);
      if ($status == 2) {
      $p['consumetime'] = TIMESTAMP;
      }
      $temp = pdo_update('laoyou_csyx_winning', $p, array('id' => $id, 'weid' => $_W['weid']));
      if ($temp == false) {
      message('抱歉，刚才操作数据失败！', '', 'error');
      } else {
      message('状态设置成功！', create_url('site/module/awardlist', array('name' => 'laoyou_csyx', 'rid' => $_GPC['rid'])), 'success');
      }
      }

      public function dogetphone() {
      global $_GPC, $_W;
      $rid = intval($_GPC['rid']);
      $fans = $_GPC['fans'];
      $tel = pdo_fetch("SELECT * FROM " . tablename('fans') . " WHERE from_user='" . $fans . "'");
      if (!$tel['mobile']) {
      echo '没有登记';
      } else {
      echo $tel['mobile'];
      }
      } */
}
